package com.ruoyi.quartz.task;

import com.ruoyi.framework.datasource.DynamicDataSource;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Component
public class ShardTableHandler {
    public static final String TABLE_NAME_PREFIX_LANE_APPLY = "laneapply";
    public static final String TABLE_NAME_PREFIX_LANE_KEY = "lanekey";
    public static final String TABLE_NAME_PREFIX_SEAT_CALL = "seatcall";
    public static final String TABLE_NAME_PREFIX_SEAT_IMAGE = "seatimage";
    @Resource
    private DynamicDataSource dataSource;

    /**
     * 按月维度进行分表-laneapply
     *
     * @param tableName
     */
    public void checkExistTableLaneApply(String tableName) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            String[] types = new String[]{"TABLE", "VIEW"};
            // 查询当前数据源下的表数据
            ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), conn.getSchema(), "%", types);
            boolean exist = false;
            // 判断是否存在
            while (rs.next()) {
                String tableStr = rs.getString("TABLE_NAME");
                if (tableStr.equals(tableName)) {
                    exist = true;
                    break;
                }
            }

            // 不存在则创建
            if (!exist) {
                System.out.println("创建lane_apply表开始...");
                // 获取文件的URL
                String tabName1 = "laneapply.sql";
                org.springframework.core.io.Resource resource = new ClassPathResource(tabName1);
                String filePath = this.getClass().getClassLoader().getResource(tabName1).getFile();
                File file1 = new File(filePath);
                // 通过流讲文件复制到file中
                FileUtils.copyToFile(resource.getInputStream(), file1);
                // 转成string输入文本
                String content1 = FileUtils.readFileToString(file1, StandardCharsets.UTF_8);
                // 获取sql
                String sql1 = String.format(content1, tableName);
                PreparedStatement ps1 = conn.prepareStatement(sql1);
                ps1.execute();
                System.out.println("创建lane_apply表结束...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 按月维度进行分表-lanekey
     *
     * @param tableName
     */
    public void checkExistTableLaneKey(String tableName) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            String[] types = new String[]{"TABLE", "VIEW"};
            // 查询当前数据源下的表数据
            ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), conn.getSchema(), "%", types);
            boolean exist = false;
            // 判断是否存在
            while (rs.next()) {
                String tableStr = rs.getString("TABLE_NAME");
                if (tableStr.equals(tableName)) {
                    exist = true;
                    break;
                }
            }

            // 不存在则创建
            if (!exist) {
                System.out.println("创建lanekey表开始...");
                // 获取文件的URL
                String tabName2 = "lanekey.sql";
                org.springframework.core.io.Resource resource = new ClassPathResource(tabName2);
                String filePath = this.getClass().getClassLoader().getResource(tabName2).getFile();
                File file2 = new File(filePath);
                // 通过流讲文件复制到file中
                FileUtils.copyToFile(resource.getInputStream(), file2);
                // 转成string输入文本
                String content2 = FileUtils.readFileToString(file2, StandardCharsets.UTF_8);
                // 获取sql
                String sql2 = String.format(content2, tableName);
                PreparedStatement ps2 = conn.prepareStatement(sql2);
                ps2.execute();
                System.out.println("创建lanekey表结束...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 按月维度进行分表-seatcall
     *
     * @param tableName
     */
    public void checkExistTableSeatCall(String tableName) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            String[] types = new String[]{"TABLE", "VIEW"};
            // 查询当前数据源下的表数据
            ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), conn.getSchema(), "%", types);
            boolean exist = false;
            // 判断是否存在
            while (rs.next()) {
                String tableStr = rs.getString("TABLE_NAME");
                if (tableStr.equals(tableName)) {
                    exist = true;
                    break;
                }
            }

            // 不存在则创建
            if (!exist) {
                System.out.println("创建seatcall表开始...");
                // 获取文件的URL
                String tabName3 = "seatcall.sql";
                org.springframework.core.io.Resource resource = new ClassPathResource(tabName3);
                String filePath = this.getClass().getClassLoader().getResource(tabName3).getFile();
                File file3 = new File(filePath);
                // 通过流讲文件复制到file中
                FileUtils.copyToFile(resource.getInputStream(), file3);
                // 转成string输入文本
                String content3 = FileUtils.readFileToString(file3, StandardCharsets.UTF_8);
                // 获取sql
                String sql3 = String.format(content3, tableName);
                PreparedStatement ps3 = conn.prepareStatement(sql3);
                ps3.execute();
                System.out.println("创建seatcall表结束...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 按月维度进行分表-seatimage
     *
     * @param tableName
     */
    public void checkExistTableSeatImage(String tableName) {
        Connection conn = null;
        try {
            conn = dataSource.getConnection();
            String[] types = new String[]{"TABLE", "VIEW"};
            // 查询当前数据源下的表数据
            ResultSet rs = conn.getMetaData().getTables(conn.getCatalog(), conn.getSchema(), "%", types);
            boolean exist = false;
            // 判断是否存在
            while (rs.next()) {
                String tableStr = rs.getString("TABLE_NAME");
                if (tableStr.equals(tableName)) {
                    exist = true;
                    break;
                }
            }

            // 不存在则创建
            if (!exist) {
                System.out.println("创建seatimage表开始...");
                // 获取文件的URL
                String tabName3 = "seatimage.sql";
                org.springframework.core.io.Resource resource = new ClassPathResource(tabName3);
                String filePath = this.getClass().getClassLoader().getResource(tabName3).getFile();
                File file3 = new File(filePath);
                // 通过流讲文件复制到file中
                FileUtils.copyToFile(resource.getInputStream(), file3);
                // 转成string输入文本
                String content3 = FileUtils.readFileToString(file3, StandardCharsets.UTF_8);
                // 获取sql
                String sql3 = String.format(content3, tableName);
                PreparedStatement ps3 = conn.prepareStatement(sql3);
                ps3.execute();
                System.out.println("创建seatimage表结束...");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
